

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>prml.nn.loss.kl &mdash; prml  documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../../_static/language_data.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> prml
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../prml.html">prml package</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">prml</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>prml.nn.loss.kl</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for prml.nn.loss.kl</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">prml.nn.function</span> <span class="k">import</span> <span class="n">Function</span>
<span class="kn">from</span> <span class="nn">prml.nn.distribution.bernoulli</span> <span class="k">import</span> <span class="n">Bernoulli</span>
<span class="kn">from</span> <span class="nn">prml.nn.distribution.categorical</span> <span class="k">import</span> <span class="n">Categorical</span>
<span class="kn">from</span> <span class="nn">prml.nn.distribution.gaussian</span> <span class="k">import</span> <span class="n">Gaussian</span>
<span class="kn">from</span> <span class="nn">prml.nn.math.log</span> <span class="k">import</span> <span class="n">log</span>
<span class="kn">from</span> <span class="nn">prml.nn.math.square</span> <span class="k">import</span> <span class="n">square</span>
<span class="kn">from</span> <span class="nn">prml.nn.nonlinear.log_softmax</span> <span class="k">import</span> <span class="n">log_softmax</span>
<span class="kn">from</span> <span class="nn">prml.nn.nonlinear.softplus</span> <span class="k">import</span> <span class="n">softplus</span>


<div class="viewcode-block" id="kl_divergence"><a class="viewcode-back" href="../../../../prml.nn.loss.html#prml.nn.loss.kl.kl_divergence">[docs]</a><span class="k">def</span> <span class="nf">kl_divergence</span><span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="n">p</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    compute sample approximation of kl divergence from p to q</span>
<span class="sd">    KL(q||p)</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    q : Distribution</span>
<span class="sd">        one generated a sample</span>
<span class="sd">    p : Distribution</span>
<span class="sd">    data : Array</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="n">Bernoulli</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">Bernoulli</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">kl_bernoulli</span><span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="n">p</span><span class="p">)</span>
    <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="n">Categorical</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">Categorical</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">kl_categorical</span><span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="n">p</span><span class="p">)</span>
    <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="n">Gaussian</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">Gaussian</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">kl_gaussian</span><span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="n">p</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">q</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">depth</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">q</span><span class="o">.</span><span class="n">log_pdf</span><span class="p">()</span> <span class="o">-</span> <span class="n">p</span><span class="o">.</span><span class="n">log_pdf</span><span class="p">(</span><span class="n">q</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">q</span><span class="o">.</span><span class="n">pdf</span><span class="p">()</span> <span class="o">*</span> <span class="p">(</span><span class="n">q</span><span class="o">.</span><span class="n">log_pdf</span><span class="p">()</span> <span class="o">-</span> <span class="n">p</span><span class="o">.</span><span class="n">log_pdf</span><span class="p">(</span><span class="n">q</span><span class="o">.</span><span class="n">data</span><span class="p">))</span></div>


<div class="viewcode-block" id="kl_bernoulli"><a class="viewcode-back" href="../../../../prml.nn.loss.html#prml.nn.loss.kl.kl_bernoulli">[docs]</a><span class="k">def</span> <span class="nf">kl_bernoulli</span><span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">q</span><span class="o">.</span><span class="n">mean</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">q</span><span class="o">.</span><span class="n">logit</span> <span class="o">-</span> <span class="n">p</span><span class="o">.</span><span class="n">logit</span><span class="p">)</span> \
        <span class="o">-</span> <span class="n">softplus</span><span class="p">(</span><span class="o">-</span><span class="n">q</span><span class="o">.</span><span class="n">logit</span><span class="p">)</span> <span class="o">+</span> <span class="n">softplus</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">logit</span><span class="p">)</span></div>


<div class="viewcode-block" id="kl_categorical"><a class="viewcode-back" href="../../../../prml.nn.loss.html#prml.nn.loss.kl.kl_categorical">[docs]</a><span class="k">def</span> <span class="nf">kl_categorical</span><span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">q</span><span class="o">.</span><span class="n">mean</span> <span class="o">*</span> <span class="p">(</span><span class="n">log_softmax</span><span class="p">(</span><span class="n">q</span><span class="o">.</span><span class="n">logit</span><span class="p">)</span> <span class="o">-</span> <span class="n">log_softmax</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">logit</span><span class="p">)))</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">axis</span><span class="o">=-</span><span class="mi">1</span><span class="p">)</span></div>


<div class="viewcode-block" id="kl_gaussian"><a class="viewcode-back" href="../../../../prml.nn.loss.html#prml.nn.loss.kl.kl_gaussian">[docs]</a><span class="k">def</span> <span class="nf">kl_gaussian</span><span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span>
    <span class="n">qvar</span> <span class="o">=</span> <span class="n">square</span><span class="p">(</span><span class="n">q</span><span class="o">.</span><span class="n">std</span><span class="p">)</span>
    <span class="n">pvar</span> <span class="o">=</span> <span class="n">square</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">std</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">log</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">std</span><span class="p">)</span> <span class="o">-</span> <span class="n">log</span><span class="p">(</span><span class="n">q</span><span class="o">.</span><span class="n">std</span><span class="p">)</span> <span class="o">+</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="p">(</span><span class="n">qvar</span> <span class="o">+</span> <span class="n">square</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">mean</span> <span class="o">-</span> <span class="n">q</span><span class="o">.</span><span class="n">mean</span><span class="p">))</span> <span class="o">/</span> <span class="n">pvar</span> <span class="o">-</span> <span class="mf">0.5</span></div>


</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, Author

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>